package project.service;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import project.mapper.StudentMapper;
import project.pojo.CourseForSelect;
import project.pojo.CourseTable;
import project.pojo.Student;
import project.util.SqlSessionFactoryUtils;
import sun.security.util.Password;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class StudentService {
    SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();

    public Student selectById(long id) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Student student = mapper.selectById(id);
        if (student!=null) student.setPassword("");
        sqlSession.close();
        return student;
    }

    public String updatePassword(long id, String oldPassword, String password){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Student student = mapper.selectById(id);
        if (student.getPassword().equals(oldPassword)){
            int res = mapper.updatePassword(id,password);
            if (res==1){
                sqlSession.commit();
                sqlSession.close();
                return "ok";
            }
            sqlSession.close();
            return "wrong";
        }else{
            sqlSession.close();
            return "wrongPassword";
        }
    }

    public String updateEmail(long id, String email){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> students = mapper.selectByEmailNum(id,email);
        if (students.size()!=0){
            sqlSession.close();
            return "same";
        }else{
            int res = mapper.updateEmail(id,email);
            if (res == 1){
                sqlSession.commit();
                sqlSession.close();
                return "ok";
            }
            sqlSession.close();
            return "wrong";
        }
    }

    public String updatePhone(long id, String phone){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> students = mapper.selectByPhoneNum(id,phone);
        if (students.size()!=0){
            sqlSession.close();
            return "same";
        }else{
            int res = mapper.updatePhone(id,phone);
            if (res == 1){
                sqlSession.commit();
                sqlSession.close();
                return "ok";
            }
            sqlSession.close();
            return "wrong";
        }
    }

    public void addStudent(Student student){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        mapper.addStudent(student);
        sqlSession.commit();
        sqlSession.close();
    }

    public Student judgeLogin(long id, String password){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Student student = mapper.judgeLogin(id, password);
        sqlSession.close();
        return student;
    }

    public List<CourseForSelect> showAllCourses(long id){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<CourseForSelect> coursesForSelect= mapper.showAllCourses(id);
        sqlSession.close();
        return coursesForSelect;
    }

    public void setValuesCourses(List<CourseForSelect> courses, long id){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<String> allTime = mapper.selectRecordTimes(id);
        for (CourseForSelect course:courses){
            if (allTime.contains(course.getCourseTime())){
                course.setBtnInfo("上课时间冲突！");
                course.setBtnType("danger");
                course.setBtnDisabled(true);
            }else{
                course.setBtnInfo("选课");
                course.setBtnType("primary");
                course.setBtnDisabled(false);
            }
        }
    }

    public List<CourseForSelect> queryAllByInformation(long userId, long id, String name, String teacherName, String type){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<CourseForSelect> courses = mapper.queryAllByInformation(userId,id,name,teacherName,type);
        sqlSession.close();
        return courses;
    }

    public int insertStudentRecord(long courseId, long studentId){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        int res = mapper.insertStudentRecord(courseId, studentId);
        sqlSession.commit();
        sqlSession.close();
        return res;
    }

    public List<CourseForSelect> showAllSelected(long studentId){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<CourseForSelect> courses = mapper.showAllSelected(studentId);
        sqlSession.close();
        return courses;
    }

    public int deleteCourseSelected(long courseId, long studentId){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        int res = mapper.deleteCourseSelected(courseId,studentId);
        sqlSession.commit();
        sqlSession.close();
        return res;
    }

    public List<CourseTable> showCourseTable(long studentId){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<CourseForSelect> courses = mapper.showAllSelected(studentId);

        String[] times = {"8:30-10:05", "10:20-12:00", "13:30-15:05", "15:20-16:50", "17:50-19:20", "19:35-21:05"};
        String[] days = {"周一","周二","周三","周四","周五","周六","周日"};

        List<CourseTable> courseTables = new ArrayList<>();
        for (String time:times){
            CourseTable courseTable = new CourseTable();
            boolean flag = false;
            for (String day:days){
                String dayTime = day + time;
                for (CourseForSelect course:courses){
                    String courseTime = course.getCourseTime();
                    if (dayTime.equals(courseTime)){
                        flag=true;
                        courseTable.setAuto(day,course.getName());
                        break;
                    }
                }
            }
            if (!flag) courseTable.setAll("     ");
            courseTables.add(courseTable);
        }

        return courseTables;
    }
}
